package again.newlife.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;

public class JsonTest {
    public static void main1(String[] args) throws Exception {

        // 将day14-code/src/again/newlife/file/person.json文件种的字符串读入程序中
        //1.创建BufferedReader对象
        BufferedReader br = new BufferedReader(new FileReader("day14-code/src/again/newlife/file/person.json"));
        //2. 准备拼接的字符串
        StringBuffer sb = new StringBuffer();
        String s = null;
        //2. 打开水龙头 循环读取
        while ((s = br.readLine()) != null) {
            sb.append(s.trim());
        }
        //3. 关闭水龙头
        br.close();

        //4.将得到的JSON字符串转换成person对象
        String s1 = sb.toString();
        System.out.println(s1);
        JSONObject json = (JSONObject) JSON.parse(s1);//先将sb转换成字符串，再转换成JSON对象
        Person person = JSON.toJavaObject(json, Person.class); // 将JSON对象转换成java对象
        System.out.println(person);

    }

    public static void main(String[] args) throws Exception {
        //1.创建读取 BufferedReader对象
        BufferedReader br = new BufferedReader(new FileReader("day14-code/src/again/newlife/file/person1.json"));
        //2.创建拼接
        StringBuffer sb = new StringBuffer();
        String s = null;
        while ((s = br.readLine()) != null){//循环读取
            sb.append(s.trim());//拼接
        }
        br.close();//关闭水龙头

        //3.将JSON字符串转换成集合对象
        String parse = sb.toString();//将JSON字符串转换成字符串
        List<Person> list = JSON.parseArray(parse, Person.class);
        System.out.println(list);

    }
}
